#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMouseEvent>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QMessageBox>
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QSqlDatabase>
#include <QJsonArray>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(555,1000);
    //设置位置
    this->setGeometry(700,25,555,1000);
    //setWindowFlag(Qt::FramelessWindowHint);//去掉标题栏

    //输出可用数据库
    qDebug()<<"available drivers:";
    QStringList drivers = QSqlDatabase::drivers();
    foreach(QString driver, drivers)
        qDebug()<<driver;

    initDatabase();
    //QString cityId = getCityId("徐州");
    //qDebug() << "cityId:" << cityId;
    QUrl url1("http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=99655893&appsecret=KRZyJ5C4&cityid=101030500");
    QUrl url2("https://www.baidu.com/");
    QNetworkRequest res1(url1);//创建请求
    QNetworkRequest res2(url2);//创建请求
    manager = new QNetworkAccessManager(this);//创建
    manager->get(res1);//发送请求
    connect(manager,&QNetworkAccessManager::finished,this,&MainWindow::replyFinished);
    this->createMap();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::RightButton){
        qDebug()<<"右键点击事件触发";
    }
    //窗口跟随左键按下移动：窗口旧位置，鼠标位置，窗口新位置
    if(event->button()==Qt::LeftButton)
    {   //获取偏移量
        moveOffset=event->globalPos()-pos();
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos()-moveOffset);
}

void MainWindow::parseJson(QByteArray data)
{
    QJsonDocument jsondoc = QJsonDocument::fromJson(data);//将JSON字符串转换为QJsonDocument对象
    if(!jsondoc.isNull()&&jsondoc.isObject()){
        QJsonObject obj = jsondoc.object();//将QJsonDocument对象转换为QJsonObject对象
        //解析日期
        QString date = obj.value("date").toString();
        QString week = obj.value("week").toString();
        //解析城市
        QString city = obj.value("city").toString();
        //解析天气
        QString wea = obj.value("wea").toString();
        //当前温度
        QString tem = obj.value("tem").toString();
         ui->Time->setText(date + " " + week);
         ui->location->setText(city+"市");
         ui->weatherType->setText(wea);
         ui->weathermain->setPixmap(imgMap[wea]);
         ui->temperatureMain->setText(tem+"℃");
         ui->temperatureMain->setStyleSheet("font-size: 30px;");
         ui->temperature->setText(obj.value("tem2").toString()+"~"+obj.value("tem1").toString()+"℃");

         ui->windMainDirection->setText(obj.value("win").toString());
         ui->windMainDirection->setAlignment(Qt::AlignCenter);
         ui->windMainSpeed->setText(obj.value("win_speed").toString());
         ui->windMainSpeed->setAlignment(Qt::AlignCenter);
         ui->air->setText(obj.value("air").toString());
         ui->air->setAlignment(Qt::AlignCenter);
         ui->airLevel->setText(obj.value("air_level").toString());
         ui->airLevel->setAlignment(Qt::AlignCenter);
         ui->humidity->setText(obj.value("humidity").toString());
         ui->humidity->setAlignment(Qt::AlignCenter);
         ui->uvIndex->setText(obj.value("uvIndex").toString());
         ui->uvIndex->setAlignment(Qt::AlignCenter);

    }
}

void MainWindow::parseJsonSeven(QByteArray data)
{
    QJsonDocument jsondoc = QJsonDocument::fromJson(data);
    if(jsondoc.isObject()&&!jsondoc.isNull()){
        QJsonObject obj = jsondoc.object();
        if(obj.contains("data")&&obj.value("data").isArray()){
            QJsonArray arr = obj.value("data").toArray();
            for(int i=0;i<arr.size();i++){
                QJsonObject obj = arr.at(i).toObject();
                Day day;
                day.city = obj.value("city").toString();
                day.day = obj.value("day").toString();
                day.data = obj.value("date").toString();
                day.week = obj.value("week").toString();
                day.wea = obj.value("wea").toString();
                day.tem = obj.value("tem").toString();
                day.tem1 = obj.value("tem1").toString();
                day.tem2 = obj.value("tem2").toString();
                day.win = obj.value("win").toString();
                day.win_speed = obj.value("win_speed").toString();
                day.air = obj.value("air").toString();
                day.air_level = obj.value("air_level").toString();
                days[i] = day;
            }
        }
    }
}
//请求处理
void MainWindow::replyFinished(QNetworkReply *reply)
{
    int resCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();//获取响应码，toInt() 是 QVariant 类的一个成员函数，用于将 QVariant 对象中存储的值转换为 int 类型。
    if(reply->error()==QNetworkReply::NoError&&resCode==200){
        qDebug()<<"请求成功";
        QByteArray data = reply->readAll();//获取数据
        //qDebug()<<"请求数据"<<QString::fromUtf8(data);
        this->parseJson(data);
    }
    else{
        qDebug()<<reply->errorString();
        QMessageBox::warning(this,"请求失败",reply->errorString(),QMessageBox::Ok);

    }
    reply->deleteLater();//释放资源
}

void MainWindow::on_pushButton_clicked()
{
    QString cityName = ui->searchContent->text();
    QString cityId = getCityId(cityName);
    qDebug()<<"cityId:"<<cityId;
    if(cityId.isEmpty()){
        QMessageBox::warning(this,"错误","未找到该城市",QMessageBox::Ok);
        return;
    }
    QUrl searchUrl("http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=99655893&appsecret=KRZyJ5C4&cityid="+cityId);
    QNetworkRequest res(searchUrl);
    manager = new QNetworkAccessManager(this);
    manager->get(res);
    connect(manager,&QNetworkAccessManager::finished,this,&MainWindow::replyFinished);
}

void MainWindow::createMap()
{
    imgMap.insert("多云",":/icons/baidu/yun.png");
    imgMap.insert("晴",":/icons/baidu/qing.png");
    imgMap.insert("阴",":/icons/baidu/yin.png");
    imgMap.insert("雨",":/icons/baidu/yu.png");
    imgMap.insert("雾",":/icons/baidu/wu.png");
    imgMap.insert("雪",":/icons/baidu/xue.png");
}





